package com.snj.visangedu.homestudy;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.snj.visangedu.R;


public class AskAdapter extends BaseAdapter {
	
	String tag = "AskAdapter";
	
	Context context;
	int layout;
	
	private int m_nPreTouchPosX = 0;
	ArrayList<AskItem> mAsksList;
	
	LayoutInflater mInflater = null;
	Fragment parentFragment = null;
	
	public AskAdapter(Context _context, int _layout, ArrayList<AskItem> _askslist) {
		// TODO Auto-generated constructor stub
		
		this.context = _context;
		this.layout = _layout; 
		this.mAsksList = _askslist;
		
	}
	
	public AskAdapter(Context _context, Fragment _fragment, int _layout, ArrayList<AskItem> _askslist) {
		// TODO Auto-generated constructor stub
		
		this.context = _context;
		this.layout = _layout;
		this.mAsksList = _askslist;
		this.parentFragment = _fragment;
		
	}
	
	public void Insert(ArrayList<AskItem> mList)
	{
		mAsksList.addAll(mList);
	}
	
	public void SetList(ArrayList<AskItem> mList)
	{
		mAsksList = mList;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return mAsksList.size();
	}

	public AskItem getItem(int position) {
		// TODO Auto-generated method stub
		return mAsksList.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
	
		final AskHolder holder;
		
		if(convertView == null)
		{
			mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = mInflater.inflate(layout, parent, false);
			
			holder = new AskHolder();
			
			holder.vf_askimgs = (ViewFlipper)convertView.findViewById(R.id.vf_askimgs);
			holder.txt_ask = (TextView)convertView.findViewById(R.id.txt_ask);
			holder.txt_askstate = (TextView)convertView.findViewById(R.id.txt_askstate);
			holder.vf_answerimgs = (ViewFlipper)convertView.findViewById(R.id.vf_answerimgs);
			
			convertView.setTag(holder);
		}
		else
		{
			holder = (AskHolder)convertView.getTag();
		}
		
		/*
		if(!mNewsList.get(position).GetDrawn())
		{
		
			Animation ani = AnimationUtils.loadAnimation(context, R.anim.listani);
			ani.setStartOffset(position * 50);
			convertView.startAnimation(ani);
			mNewsList.get(position).SetDrawn(true);
		}
		
		Log.d(tag, "Created news item");
		*/ 
		
		
		holder.txt_ask.setText(mAsksList.get(position).GetAskContent());
		
		ArrayList<Bitmap> arr_askimgs = mAsksList.get(position).GetAskImgs();
		if(arr_askimgs != null)
		{
			holder.vf_askimgs.removeAllViews();
			for(int i=0; i<arr_askimgs.size(); i++)
			{
				ImageView img_askimg = new ImageView(context);
				img_askimg.setImageBitmap(arr_askimgs.get(i));
				holder.vf_askimgs.addView(img_askimg);
			}
		}
		
		
		if(mAsksList.get(position).GetAnswerImgs() == null || mAsksList.get(position).GetAnswerImgs().size() <= 0)
		{
			holder.vf_answerimgs.setVisibility(View.GONE);
		}
		else
		{
			holder.vf_answerimgs.setVisibility(View.VISIBLE);
			holder.vf_answerimgs.removeAllViews();
			
			ArrayList<Bitmap> arr_answerimgs = mAsksList.get(position).GetAnswerImgs();
			for(int i=0; i<arr_answerimgs.size(); i++)
			{
				ImageView img_askimg = new ImageView(context);
				img_askimg.setImageBitmap(arr_answerimgs.get(i));
				holder.vf_answerimgs.addView(img_askimg);
			}
		}
		
		if(!mAsksList.get(position).GetState().equals(""))
		{
			holder.txt_askstate.setText(mAsksList.get(position).GetState());
			holder.txt_askstate.setTag(mAsksList.get(position).GetNo());
			holder.txt_askstate.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if(parentFragment == null) return;
					
					
					TextView txt_askstate = (TextView)v;
					
					if(txt_askstate.getText().toString().equals("답변하기"))
					{
						((TeacherFragment)parentFragment).Answer(v.getTag().toString());
					}
					else if(txt_askstate.getText().toString().equals("선택"))
					{
						((TeacherFragment)parentFragment).ChooseAsk(v.getTag().toString());
					}
					
					
				}
			});
		}
		
		holder.vf_askimgs.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				
				if (event.getAction() == MotionEvent.ACTION_DOWN)
	    		{
	    			m_nPreTouchPosX = (int)event.getX();
	    		}
	    		
	    		if (event.getAction() == MotionEvent.ACTION_UP)
	    		{
	    			int nTouchPosX = (int)event.getX();
	    			ViewFlipper vf = (ViewFlipper)v;
	    			
	    			if (nTouchPosX < m_nPreTouchPosX)
	    			{
	    				MoveNextView(vf);
	    			}
	    			else if (nTouchPosX > m_nPreTouchPosX)
	    			{
	    				MovewPreviousView(vf);
	    			}
	    			
	    			m_nPreTouchPosX = nTouchPosX;
	    		}
	    		
	            return true;
			}
		});
		
		
		return convertView;
	}
	
	private void MoveNextView(ViewFlipper vf_imgs)
    {	
		vf_imgs.setInAnimation(AnimationUtils.loadAnimation(context,	R.anim.appear_from_right));
		vf_imgs.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.disappear_to_left));
		vf_imgs.showNext();
    }
	
	private void MovewPreviousView(ViewFlipper vf_imgs)
    {    	
    	
		vf_imgs.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.appear_from_left));
		vf_imgs.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.disappear_to_right));
    	vf_imgs.showPrevious();
    }
	
	
}
